'''

pip install pycomm3
'''
import traceback

try:
    from plc_client.layer1.abc_client import PLCClientInterface
except:
    from abc_client import PLCClientInterface
# 绝对地址通讯； 标签通讯方式

#
# from pycomm import PLC
#
#
# def main():
#     # 设置PLC的IP地址
#     ip_address = "192.168.1.1"
#     # Ethernet/IP默认端口
#     port = 96952
#
#     try:
#         # 创建PLC实例
#         plc = PLC(ip_address, port)
#
#         # 连接到PLC
#         plc.open()
#
#         # 登录录入PLC，如果需要的话（根据PLC配置）
#         # plc.login("username", "password")
#
#         # 读取数据，例如读取D100
#         d10_data = plc.read_tag("D10")
#         print(f"Read D100: {d10_data}")
#
#         # 写入数据示例，写入D101设为100
#         plc.write_tag("D10", 100)
#         print("Write D10 done.")
#     except Exception as e:
#         print(f"Error: {e}")
#     finally:
#         # 关闭连接
#         plc.close()
# if __name__ == "__main__":
#     main()



#====== python通信代码：=========
'''
pip install pycomm3
'''
# from pycomm3 import CIPDriver
# with CIPDriver('192.168.250.12') as drive:
#     print(drive)
#
#
# from pycomm3 import CIPDriver
# CIPDriver.list_identity('192.168.250.1')
#
# from pycomm3 import CIPDriver
# CIPDriver.discover()

from pycomm3 import LogixDriver

plc1 = LogixDriver('192.168.250.1')

# Create a LogixDriver instance
with LogixDriver('192.168.250.1') as plc:

    # Read tag values
    tag_values = plc.read('Tag1', 'Tag2')   # adjust the tag names and values as needed

    # Write tag values
    plc.write('Tag1', 100)

#
# class EthIPClient(PLCClientInterface): # EtherNet/Ip (cip)
#
#     def __init__(self, ip='127.0.0.1', port=1102):
#         # 创建通讯客户端实例
#         # self.client = modbus_tcp.TcpMaster(host=ip, port=port, timeout_in_sec=3.0)
#         self.client = None
#         self.ip = ip
#         self.port = port
#
#
#     def connect(self):
#         # 连接至PLC
#         try:
#             self.client = LogixDriver(self.ip)
#         except:
#             print(traceback.format_exc())
#         # print(f"客户端 是否连接到 plc服务端：{self.client.get_connected()}")
#
#     def get_connected(self):
#         # return self.client.get_connected()
#         pass
#
#     def read_int8(self, start=0, size=1):
#
#         tag = self.client.read('DINT1') # Tag(tag='DINT1', value=20, type='DINT', error=None)
#
#         return tag.value
#
#     def read_int16(self, start=0, size=2):  # 读int16
#         pass
#
#     def read_bool(self, start=0, size=1): # 读bool
#         pass
#
#     def read_real(self, start=0, size=4): # 读实数
#         pass
#
#     def write_int8(self, start=0, data_int8=0): # 写int8
#         self.client[start] = data_int8
#
#     def write_int16(self, start=0, data_int8=0): #
#         pass
#
#     def write_real(self, start=0, data_real=1.0): # 写实数  用于发送坐标等
#         pass
#
#     def write_bool(self, start=0, data_bool=False): # 写bool
#         pass
#
#     def read_string(self, start=0, size=50, dbnumber=0): # 读 string  size=256
#         pass
#
#     def write_string(self, start=0, data_str='hello', dbnumber=0): # write string
#         pass
#
#     def close(self):
#         self.client= None
#         # print(f"plc 连接状态：{self.client.get_connected()}")
#

# def test_SimClient():
#     ly1_client = SimClient()
#     ly1_client.connect()
#     ly1_client.write_int8(start=10, data_int8=100)
#     value = ly1_client.read_int8(start=10)
#     print(value)
#     ly1_client.close()
#
# if __name__ == "__main__":
#     test_SimClient()

